package compiler;

import XMLapps.XMLParser;
import XMLapps.XMLGenerator;
import Interpreter.InterpreterVisitor;
import ast.*;
import parser.Parser;
import visitor.*;

/**
 *  The Compiler class contains the main program for compiling
 *  a source program to bytecodes
*/
public class Compiler {

/**
 * The Compiler class reads and compiles a source program
*/
    int patientId;	
    String sourceFile;
	
    public Compiler(String sourceFile) {
    	this.sourceFile = sourceFile;
        patientId = 0;
    }
    
    public Compiler(String sourceFile, int patientId) {
        this.sourceFile = sourceFile;
        this.patientId = patientId;
    }
    
    void compileProgram() {
        try {
            Parser parser = new Parser(sourceFile);
            AST t = parser.execute();
            PrintVisitor pv = new PrintVisitor();
            t.accept(pv);
            System.out.println("\n\n--------------\n\n");
            XMLGenerator xg = new XMLGenerator();
            t.accept(xg);
            XMLParser xp =new XMLParser();
            xp.ReadX();
            InterpreterVisitor iv = new InterpreterVisitor(patientId);
            t.accept(iv);
            
            
           // xp.ReadXMLFile();
            
        }catch (Exception e) {
            System.out.println("********exception*******"+e.toString());
         };
    }
    
    public static void main(String args[]) {
        if (args.length == 0) {
            System.out.println("***Incorrect usage, try: java compiler.Compiler <file>");
            System.exit(1);
        }
        (new Compiler(args[0])).compileProgram();
    }
}
